package com.seaking.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by wchha on 10/25 0025.
 */
public class LoginFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest servletRequest1 = (HttpServletRequest) servletRequest;
        HttpServletResponse servletResponse1 = (HttpServletResponse) servletResponse;
        HttpSession session = servletRequest1.getSession();
        String userName = (String) session.getAttribute("userName");
        StringBuffer requestURI = servletRequest1.getRequestURL();
        if (userName != null || requestURI.indexOf("login") > 0 || requestURI.indexOf("verification") > 0 || requestURI.indexOf("static") > 0 || requestURI.indexOf("register") > 0) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        } else {
            servletResponse1.sendRedirect("/login");
        }
    }

    public void destroy() {

    }
}
